package com.blog.controller;

import com.blog.constant.Types;
import com.blog.constant.WebConst;
import com.blog.dto.MetaDto;
import com.blog.dto.cond.ContentCondition;
import com.blog.model.User;
import com.blog.service.meta.MetaService;
import com.blog.utils.MapCache;
import com.blog.utils.TaleUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author wmb
 *
 * @since 2021-2-8
 */
public abstract class BaseController {

    protected MapCache cache = MapCache.single();

    @Autowired
    @Qualifier("metaService")
    private MetaService metaService;

    /**
     * 获取请求绑定的登录对象
     *
     * @param request
     *
     * @return
     */
    public User user(HttpServletRequest request){
        return TaleUtils.getLoginUser(request);
    }

    /**
     * 获取用户id
     *
     * @param request
     *
     * @return
     */
    public Integer getUid(HttpServletRequest request) {
        return this.user(request).getUid();
    }

    /**
     * 获取blog页面需要的公共数据
     *
     * @param request
     * @param contentCondition
     *
     * @return
     */
    public BaseController blogBaseData(HttpServletRequest request, ContentCondition contentCondition) {
        List<MetaDto> metaDtoList = metaService.getMetaList(Types.LINK.getType(), null, WebConst.MAX_POSTS);
        request.setAttribute("links", metaDtoList);
        return this;
    }
}
